package com.money.activemq;

import com.money.dao.MyOrderMapper;
import com.money.demo.utils.Contains;
import com.money.domain.MyOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import java.io.Serializable;

/**
 *  未支付订单监听：
 * @author
 *
 */
@Component
public class ListenerOrder implements MessageListener {
    @Resource
    private MyOrderMapper orderMapper;


    @Override
    public void onMessage(Message message) {
        // 接收Object消息
            if (message instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) message;
                try {
                    Serializable serializable = objectMessage.getObject();
                    MyOrder order =(MyOrder)serializable;
                    //查询数据库订单状态是否是未支付状态
                    MyOrder myOrder = orderMapper.selectByPrimaryKey(order.getId());
                    if(myOrder.getIsEffective() == Contains.ORDER_TYPE_LEVEL1){
                        //未支付订单发送通知用户
                        ActiveMQTopicProducer.sendMessageByTopic("D",(Serializable)myOrder,0L);
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
//        TextMessage textMessage = (TextMessage) message;
//        System.out.println(textMessage.toString());
    }
}

